import adminAPI from "@/api/adminAPI";

export default {
  state: {
    isselect: false, //是否刷新
    ListAllData: [],
    ListData: [],
    selectionList: [], // 当前选择的结果
    dataCount: 0, // 总条数
    pageSize: 10, // 每页显示多少条
    pageCurrent: 1, // 当前显示第几页
    pageShowsizer: true,
    pageShowTotal: true,
    pageShowElevator: true,
    pageSizeOpts: [5, 10, 20, 30, 40],
    orderby: "desc",
    sortby: "Count",
    searchValue: "",
    searchKey: "Name",
    tableLoading: false,
  },
  getters: {},
  actions: {
    searchCustomList({ commit, state }) {
      commit("setCustomList", {
        tableLoading: false,
      });
      adminAPI
        .Search(
          `customname_table`,
          true,
          [],
          [state.sortby],
          [state.orderby],
          (state.pageCurrent - 1) * state.pageSize,
          state.pageCurrent * state.pageSize,
          [],
          []
        )
        .then((d) => {
          if (d.code === 2000 && d.data) {
            commit("setCustomList", {
              dataCount: d.data.count,
              tableLoading: true,
            });
          }
        });
      return adminAPI
        .Search(
          `customname_table`,
          false,
          [],
          [state.sortby],
          [state.orderby],
          state.pageSize,
          state.pageCurrent,
          [],
          []
        )
        .then((d) => {
          if (d.code === 2000 && d.data) {
            d.data[0].selected = true;
            commit("setCustomList", {
              ListData: d.data,
              tableLoading: true,
            });
            return d.data;
          }
        });
    },
    addCustomList({ commit, state }, data) {
      console.log("__data__", data);
      return adminAPI.Add(`customname_table`, data);
    },
    editCustomList({ commit, state }, data) {
      console.log("_data__", data);
      return adminAPI.Update("customname_table", data.Id, data);
    },
    deleteCustomList({ commit, state }, Id) {
      return adminAPI.Delete("customname_table", Id, {});
    },
  },
  mutations: {
    setCustomList(
      state,
      { isselect, ListAllData, ListData, dataCount, pageSize, pageCurrent }
    ) {
      if (isselect != undefined) state.isselect = isselect;
      if (ListAllData != undefined) state.ListAllData = ListAllData;
      if (ListData != undefined) state.ListData = ListData;
      if (dataCount != undefined) state.dataCount = dataCount;
      if (pageSize != undefined) state.pageSize = pageSize;
      if (pageCurrent != undefined) state.pageCurrent = pageCurrent;
    },
  },
};
